# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : alarm_view.py
# Time       ：2023/11/2 10:33
# Author     ：YangYong
# version    ：python 3.10
# Description：告警视图
"""
from flask import request
from flask_restx import fields

from apis.notice import notice_ns
from apis.notice.model.alarm import Alarm
from apis.notice.schema.alarm_schema import AlarmSchema, AlarmMessageSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_model import db
from public.base_response import MarshalModel, generate_response
from public.base_view import BaseView, BaseDetailsView

alarm_model = {
    'process_name': fields.String(description='流程名称'),
    'process_id': fields.String(description='流程ID'),
    'process_instance_id': fields.String(description='流程实例ID'),
    'task_id': fields.String(description='任务ID'),
    'task_name': fields.String(description='任务名称'),
    'error_info': fields.String(description='错误信息'),
    'people_confirm': fields.Boolean(description='是否确认'),
}
alarm_message_item = notice_ns.model(
    'alarm_message_item',
    {
        'id': fields.Integer(),
        'process_name': fields.String(),
        'insert_time': fields.String(),

    }
)
alarm_message_model = {
    'total': fields.Integer(),
    'alarm_message': fields.Nested(alarm_message_item, as_list=True)

}
alarm_marshal = MarshalModel('alarm', notice_ns)


class AlarmDetailsView(BaseDetailsView):
    url = '/alarm/detail'

    def __init__(self, *args, **kwargs):
        super(AlarmDetailsView, self).__init__(*args, **kwargs)

    @handle_api_error()
    @notice_ns.doc(description="获取告警详情")
    @notice_ns.expect(alarm_marshal.detail_request())
    @notice_ns.marshal_with(alarm_marshal.detail_response(alarm_model))
    def get(self):
        """获取告警详情"""
        return super(AlarmDetailsView, self).get()


class AlarmMessageView(BaseDetailsView):
    url = '/alarm_message'

    def __init__(self, *args, **kwargs):
        super(AlarmMessageView, self).__init__(*args, **kwargs)

    @handle_api_error()
    @notice_ns.doc(description="获取告警通知")
    @notice_ns.marshal_with(alarm_marshal.detail_response(alarm_message_model, is_db=False))
    def get(self):
        objs = db.session.query(Alarm.id, Alarm.process_name, Alarm.insert_time).filter(
            Alarm.active == 1, Alarm.people_confirm.isnot(True)).all()
        alarm_message = AlarmMessageSchema().dump(objs, many=True)

        return generate_response({'alarm_message': alarm_message, 'total': len(alarm_message)})


class AlarmView(BaseView):
    url = '/alarm/'

    def __init__(self, *args, **kwargs):
        super(AlarmView, self).__init__(*args, **kwargs)
        self.table_orm = Alarm
        self.table_schema = AlarmSchema

    def handle_response_data(self):
        if request.method == 'POST':
            raise ValueError('告警不支持手动创建')

    @handle_api_error()
    @notice_ns.doc(description="获取告警列表")
    @notice_ns.expect(alarm_marshal.list_request(alarm_model))
    @notice_ns.marshal_with(alarm_marshal.list_response(alarm_model))
    def get(self):
        """获取告警列表"""
        return super(AlarmView, self).get()

    @handle_api_error()
    @notice_ns.doc(description="修改告警")
    @notice_ns.expect(alarm_marshal.update_request(alarm_model))
    @notice_ns.marshal_with(alarm_marshal.update_response())
    def put(self):
        """修改告警"""
        return super(AlarmView, self).put()

    @handle_api_error()
    @notice_ns.doc(description="删除告警")
    @notice_ns.expect(alarm_marshal.delete_request())
    @notice_ns.marshal_with(alarm_marshal.delete_response())
    def delete(self):
        """删除告警"""
        return super(AlarmView, self).delete()

    @notice_ns.hide
    def post(self):
        return super(AlarmView, self).post()
